package com.winz.filterbar;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.interpolator.view.animation.LinearOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

/**
 * Created by terry
 */

public class FilterTip extends FrameLayout implements FilterBar.OnTabSelectedChangeListener {

    private boolean bottomTip; // 是否需要底部导航
    private View tipView;
    private int tip_width = 50;
    private int tip_height = 50;

    private int last = 0;//tipView最后所在位置的坐标

    private int icon_tip;

    public FilterTip(@NonNull Context context) {
        this(context, null);
    }

    public FilterTip(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FilterTip);
        bottomTip = typedArray.getBoolean(R.styleable.FilterTip_bottomTip, false);
        tip_width = (int) typedArray.getDimension(R.styleable.FilterTip_tip_width, 25);
        tip_height = (int) typedArray.getDimension(R.styleable.FilterTip_tip_height, 25);
        icon_tip = typedArray.getResourceId(R.styleable.FilterTip_tip_img,R.drawable.icon_tip);
        typedArray.recycle();
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        syncFilterTabState();
    }

    private void syncFilterTabState() {
        if (getChildCount() > 0) {
            View child = getChildAt(0);
            if (child instanceof FilterBar) {
                ((FilterBar) child).addTabSelectedChangeListener(FilterTip.this);
            }
        }

        if (bottomTip) {
            tipView = new View(getContext());
            LayoutParams tipLayoutParams = new LayoutParams(tip_width, tip_height);
            tipLayoutParams.gravity = Gravity.BOTTOM;
            tipView.setLayoutParams(tipLayoutParams);
            tipView.setBackgroundResource(icon_tip);
            tipView.setVisibility(GONE);
            addView(tipView);
        }

    }


    @Override
    public void onChange(FilterTab filterTab, boolean selected, int flag) {
        if (bottomTip) {
            if (selected) {
                if (tipView.getVisibility() == GONE) {
                    tipView.setVisibility(VISIBLE);
                }
                // 改变tipView的左边距
                changeMargin(last, (int) (filterTab.getX() + filterTab.getWidth() / 2 - tipView.getWidth()/2));

            } else {
                tipView.setVisibility(GONE);
//                if (flag == FilterTab.FLAG_CLICK) {
//                    tipView.setVisibility(GONE);
//                }
            }
        }
    }

    private void changeMargin(int start, int end) {

        ObjectAnimator translationX = new ObjectAnimator().ofFloat(tipView, "translationX", start, end);
        translationX.setInterpolator(new LinearOutSlowInInterpolator());
        translationX.setDuration(200);
        translationX.start();
        last = end;

    }
}
